/**
 * jims
 */
package com.jims.master.modules.doctor.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.ApplyDrugWithdra;
import com.jims.master.modules.doctor.service.ApplyDrugWithdraService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ApplyDrugWithdraController
 * @author zp
 * @version 2017-07-21
 */
@Controller
@RequestMapping(value = "${adminPath}/applydrugwithdra")
public class ApplyDrugWithdraController extends BaseController {

	@Autowired
	private ApplyDrugWithdraService applyDrugWithdraService;
	
	@ModelAttribute
	public ApplyDrugWithdra get(@RequestParam(required=false) String id) {
		ApplyDrugWithdra entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = applyDrugWithdraService.get(id);
		}
		if (entity == null){
			entity = new ApplyDrugWithdra();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(ApplyDrugWithdra applyDrugWithdra, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ApplyDrugWithdra> page = applyDrugWithdraService.findPage(new Page<ApplyDrugWithdra>(request, response), applyDrugWithdra); 
		model.addAttribute("page", page);
		return "modules/orders/inpBillDetailRefundList";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(ApplyDrugWithdra applyDrugWithdra, Model model) {
		model.addAttribute("applyDrugWithdra", applyDrugWithdra);
		return "modules/orders/inpBillDetailRefundList";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(ApplyDrugWithdra applyDrugWithdra, Model model) {
	    StringData data=new StringData();
		int i=applyDrugWithdraService.saveApplyDrugWithdra(applyDrugWithdra);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "saveAll")
	@ResponseBody
	public StringData saveAll(ApplyDrugWithdra applyDrugWithdra, Model model) {
	    StringData data=new StringData();
		applyDrugWithdra.preInsert();
		applyDrugWithdra.setRetType("1");
		applyDrugWithdra.updateOrgId();
		int i=applyDrugWithdraService.saveApplyDrugWithdraAll(applyDrugWithdra.getList());
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(ApplyDrugWithdra applyDrugWithdra) {
	    StringData data=new StringData();
		int i=applyDrugWithdraService.delete(applyDrugWithdra);
        if(i>0){
            data.setCode("success");
            data.setData("删除ApplyDrugWithdra成功");
        }else{
            data.setCode("error");
            data.setData("删除ApplyDrugWithdra失败");
        }
		return data;
	}


	@RequiresPermissions("user")
	@RequestMapping(value = "applyForWithdrawal")
	public String applyForWithdrawal(ApplyDrugWithdra applyDrugWithdra,Model model){
		applyDrugWithdra.preInsert();
		applyDrugWithdra.setRetAmount("");
		applyDrugWithdra.setRetType("1");
		model.addAttribute("applyDrugWithdra",applyDrugWithdra);
		return "modules/orders/applyRetDrugAmount";
	}

}